package com.gxa.service.impl;

import com.gxa.common.JsonResult;
import com.gxa.dao.CustomerDao;
import com.gxa.dao.impl.CustomerDaoImpl;
import com.gxa.pojo.CareRemind;
import com.gxa.pojo.Customer;
import com.gxa.service.CustomerService;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CustomerServiceImpl implements CustomerService {
    //创建dao对象
    CustomerDao customerDao = new CustomerDaoImpl();

    @Override
    public JsonResult queryCustomer() {
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        try {
            //调用dao
            List<Customer> list = customerDao.queryCustomer();
            Integer count = customerDao.countCustomer();
            if(list!=null) {
                for (Customer customer : list) {
                    List orderList = customerDao.queryOrder(customer.getCustomer_name());
                    customer.setConsume_times(orderList.size());
                }
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
                jsonResult.setCount(count);
                jsonResult.setData(list);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(501);
            jsonResult.setMsg("服务器开小差，请联系管理员");
        }
        return jsonResult;
    }

    /**
     * 查询客户信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public JsonResult queryCustomer(Integer currentPage, Integer pageSize) {
        int param01 = (currentPage-1)*pageSize;
        int param02 = pageSize;
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        Integer count = 0;
        try {
            //调用dao
            List list = customerDao.queryCustomer(param01, param02);
            count = list.size();
            if(list!=null) {
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
                jsonResult.setCount(count);
                jsonResult.setData(list);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(501);
            jsonResult.setMsg("服务器开小差，请联系管理员");
        }
        return jsonResult;
    }

    /**
     * 客户满意度管理：按客户名搜索客户满意度信息
     * @param currentPage
     * @param pageSize
     * @param customer_name
     * @return
     */
    @Override
    public JsonResult searchCustomer(Integer currentPage, Integer pageSize, String customer_name) {
        int param01 = (currentPage-1)*pageSize;
        int param02 = pageSize;
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        try {
            //调用dao
            List list = customerDao.searchCustomer(param01, param02, customer_name);
            int count = 0;
            if(list!=null) {
                count = list.size();
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
                jsonResult.setCount(count);
                jsonResult.setData(list);
            }else {
                jsonResult.setCode(0);
                jsonResult.setMsg("未查询到数据");
                jsonResult.setCount(count);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(501);
            jsonResult.setMsg("服务器开小差，请联系管理员");
        }
        return jsonResult;
    }

    /**
     * 按条件搜索客户信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public JsonResult searchCustomer(Integer currentPage, Integer pageSize, String customer_name, Long mobile_phone) {
        int param01 = (currentPage-1)*pageSize;
        int param02 = pageSize;
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        try {
            //调用dao
            List list = customerDao.searchCustomer(param01, param02, customer_name, mobile_phone);
            int count = 0;
            if(list!=null) {
                count = list.size();
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
                jsonResult.setCount(count);
                jsonResult.setData(list);
            }else {
                jsonResult.setCode(0);
                jsonResult.setMsg("未查询到数据");
                jsonResult.setCount(count);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(501);
            jsonResult.setMsg("服务器开小差，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public JsonResult insertCustomer(Customer customer) {
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        try {
            //调用dao
            Integer result = customerDao.insertCustomer(customer);
            if(result>0){
                jsonResult.setCode(200);
                jsonResult.setMsg("添加成功");
                jsonResult.setData(result);
            }else {
                jsonResult.setCode(500);
                jsonResult.setMsg("添加失败");
                jsonResult.setData(result);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(501);
            jsonResult.setMsg("服务器开小差，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public JsonResult deleteCustomer(Integer id) {
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        try {
            //调用dao
            Integer result = customerDao.deleteCustomer(id);
            if(result>0){
                jsonResult.setCode(200);
                jsonResult.setMsg("删除成功");
                jsonResult.setData(result);
            }else {
                jsonResult.setCode(500);
                jsonResult.setMsg("删除失败");
                jsonResult.setData(result);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(501);
            jsonResult.setMsg("服务器开小差，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public JsonResult batchDelService(String[] ids) {
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        try {
            if(ids.length>0){
                for (String id : ids) {
                    int result = customerDao.deleteCustomer(Integer.parseInt(id));
                }
            }
            jsonResult.setCode(200);
            jsonResult.setMsg("删除成功");
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public JsonResult updateCustomer(Customer customer) {
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        try {
            //调用dao
            Integer result = customerDao.updateCustomer(customer);
            if(result>0){
                jsonResult.setCode(200);
                jsonResult.setMsg("修改成功");
                jsonResult.setData(result);
            }else {
                jsonResult.setCode(500);
                jsonResult.setMsg("修改失败");
                jsonResult.setData(result);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(501);
            jsonResult.setMsg("服务器开小差，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public JsonResult queryCustomerSatisfaction(Integer currentPage, Integer pageSize){
        int param01 = (currentPage-1)*pageSize;
        int param02 = pageSize;
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        int count = 0;
        try {
            //调用dao
            List<Customer> list = customerDao.queryCustomerSatisfaction(param01, param02);
            count = list.size();
            if(list!=null) {
                for (Customer customer : list) {
                    List orderList = customerDao.queryOrder(customer.getCustomer_name());
                    customer.setConsume_times(orderList.size());
                    customer.setOrderList(orderList);
                }
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
                jsonResult.setCount(count);
                jsonResult.setData(list);
            }else {
                jsonResult.setCode(500);
                jsonResult.setMsg("查询失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(501);
            jsonResult.setMsg("服务器开小差，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public JsonResult addCustomerSatisfaction(Customer customer) {
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        try {
            //调用dao
            Integer result = customerDao.addCustomerSatisfaction(customer);
            if(result > 0) {
                jsonResult.setCode(0);
                jsonResult.setMsg("添加成功");
            }else {
                jsonResult.setCode(500);
                jsonResult.setMsg("添加失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(501);
            jsonResult.setMsg("服务器开小差，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public JsonResult updateCustomerSatisfaction(Customer customer) {
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        try {
            //调用dao
            Integer result = customerDao.updateCustomerSatisfaction(customer);
            if(result>0){
                jsonResult.setCode(200);
                jsonResult.setMsg("修改成功");
                jsonResult.setData(result);
            }else {
                jsonResult.setCode(500);
                jsonResult.setMsg("修改失败");
                jsonResult.setData(result);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(501);
            jsonResult.setMsg("服务器开小差，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public JsonResult queryCustomerCareRemind(Long customer_code, Integer currentPage, Integer pageSize) {
        int param01 = (currentPage-1)*pageSize;
        int param02 = pageSize;
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        try {
            //调用dao
            List<CareRemind> list = customerDao.queryCustomerCareRemind(customer_code,param01,param02);
            if(list.size()>0){
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
                jsonResult.setCount(list.size());
                jsonResult.setData(list);
            }else {
                jsonResult.setCode(500);
                jsonResult.setMsg("查询失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(501);
            jsonResult.setMsg("服务器开小差，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public JsonResult addCustomerCareRemind(CareRemind careRemind) {
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        try {
            //调用dao
            Integer result = customerDao.addCustomerCareRemind(careRemind);
            if(result > 0) {
                jsonResult.setCode(200);
                jsonResult.setMsg("添加成功");
            }else {
                jsonResult.setCode(500);
                jsonResult.setMsg("添加失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(501);
            jsonResult.setMsg("服务器开小差，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public JsonResult updateCustomerCareRemind(CareRemind careRemind) {
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        try {
            //调用dao
            Integer result = customerDao.updateCustomerCareRemind(careRemind);
            if(result > 0) {
                jsonResult.setCode(200);
                jsonResult.setMsg("修改成功");
            }else {
                jsonResult.setCode(500);
                jsonResult.setMsg("修改失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(501);
            jsonResult.setMsg("服务器开小差，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public JsonResult deleteCustomerCareRemind(String[] ids) {
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        try {
            if(ids.length>0){
                for (String id : ids) {
                    int result = customerDao.deleteCustomerCareRemind(Integer.parseInt(id));
                }
            }
            jsonResult.setCode(200);
            jsonResult.setMsg("删除成功");
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public JsonResult searchCustomerCareRemind(Integer currentPage, Integer pageSize, String customer_name) {
        int param01 = (currentPage-1)*pageSize;
        int param02 = pageSize;
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        try {
            //调用dao
            List list = customerDao.searchCustomerCareRemind(param01, param02, customer_name);
            int count = 0;
            if(list!=null) {
                count = list.size();
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
                jsonResult.setCount(count);
                jsonResult.setData(list);
            }else {
                jsonResult.setCode(0);
                jsonResult.setMsg("未查询到数据");
                jsonResult.setCount(count);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(501);
            jsonResult.setMsg("服务器开小差，请联系管理员");
        }
        return jsonResult;
    }
}
